import request from '@/utils/request'

// 模拟类别数据
const categoryData = [
  {
    id: 1,
    name: '寿衣',
    code: 'SY',
    status: 1,
    sort: 1,
    products: [
      {
        id: 101,
        name: '高档寿衣-男',
        price: 1288.00,
        stock: 50,
        status: 1
      },
      {
        id: 102,
        name: '高档寿衣-女',
        price: 1288.00,
        stock: 45,
        status: 1
      },
      {
        id: 103,
        name: '中档寿衣-男',
        price: 888.00,
        stock: 60,
        status: 1
      },
      {
        id: 104,
        name: '中档寿衣-女',
        price: 888.00,
        stock: 55,
        status: 1
      }
    ]
  },
  {
    id: 2,
    name: '骨灰盒',
    code: 'GHH',
    status: 1,
    sort: 2,
    products: [
      {
        id: 201,
        name: '豪华骨灰盒',
        price: 3888.00,
        stock: 20,
        status: 1
      },
      {
        id: 202,
        name: '精品骨灰盒',
        price: 2888.00,
        stock: 30,
        status: 1
      },
      {
        id: 203,
        name: '标准骨灰盒',
        price: 1888.00,
        stock: 40,
        status: 1
      }
    ]
  },
  {
    id: 3,
    name: '花圈',
    code: 'HQ',
    status: 1,
    sort: 3,
    products: [
      {
        id: 301,
        name: '特大花圈',
        price: 688.00,
        stock: 25,
        status: 1
      },
      {
        id: 302,
        name: '大花圈',
        price: 488.00,
        stock: 35,
        status: 1
      },
      {
        id: 303,
        name: '中花圈',
        price: 288.00,
        stock: 45,
        status: 1
      },
      {
        id: 304,
        name: '小花圈',
        price: 188.00,
        stock: 55,
        status: 1
      }
    ]
  },
  {
    id: 4,
    name: '祭品',
    code: 'JP',
    status: 1,
    sort: 4,
    products: [
      {
        id: 401,
        name: '祭品套装-豪华',
        price: 1688.00,
        stock: 15,
        status: 1
      },
      {
        id: 402,
        name: '祭品套装-标准',
        price: 988.00,
        stock: 25,
        status: 1
      },
      {
        id: 403,
        name: '纸钱',
        price: 88.00,
        stock: 100,
        status: 1
      }
    ]
  },
  {
    id: 5,
    name: '棺木',
    code: 'GM',
    status: 1,
    sort: 5,
    products: [
      {
        id: 501,
        name: '豪华棺木',
        price: 8888.00,
        stock: 10,
        status: 1
      },
      {
        id: 502,
        name: '精品棺木',
        price: 6888.00,
        stock: 15,
        status: 1
      },
      {
        id: 503,
        name: '标准棺木',
        price: 4888.00,
        stock: 20,
        status: 1
      }
    ]
  }
]

// 获取类别列表
export function getCategoryList() {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 确保每个类别都有排序号
      categoryData.forEach((category, index) => {
        if (!category.sort) {
          category.sort = index + 1
        }
      })

      resolve({
        code: 0,
        data: categoryData.map(({ products, ...rest }) => rest),
        message: 'success'
      })
    }, 300)
  })
}

// 更新类别
export function updateCategory(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const index = categoryData.findIndex(item => item.id === data.id)
      if (index > -1) {
        Object.assign(categoryData[index], data)
      }
      resolve({
        code: 0,
        data: null,
        message: 'success'
      })
    }, 300)
  })
}

// 删除类别
export function deleteCategory(id) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const index = categoryData.findIndex(item => item.id === id)
      if (index > -1) {
        categoryData.splice(index, 1)
      }
      resolve({
        code: 0,
        data: null,
        message: 'success'
      })
    }, 300)
  })
}

// 更新类别排序
export function updateCategorySort(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const { id, oldIndex, newIndex } = data
      // 获取当前移动的项
      const movingItem = categoryData[oldIndex]

      // 更新所有受影响项的排序
      if (oldIndex < newIndex) {
        // 向后移动：中间项的排序号减1
        for (let i = oldIndex + 1; i <= newIndex; i++) {
          categoryData[i].sort = i
        }
      } else {
        // 向前移动：中间项的排序号加1
        for (let i = newIndex; i < oldIndex; i++) {
          categoryData[i].sort = i + 2
        }
      }

      // 移动当前项到新位置
      categoryData.splice(oldIndex, 1)
      categoryData.splice(newIndex, 0, movingItem)
      // 设置当前项的新排序号
      movingItem.sort = newIndex + 1

      resolve({
        code: 0,
        data: null,
        message: 'success'
      })
    }, 300)
  })
}

// 更新商品排序
export function updateProductSort(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const { categoryId, productId, oldIndex, newIndex } = data
      const category = categoryData.find(item => item.id === categoryId)

      if (category && category.products) {
        // 获取当前移动的商品
        const movingProduct = category.products[oldIndex]

        // 更新所有受影响商品的排序
        if (oldIndex < newIndex) {
          // 向后移动：中间项的排序号减1
          for (let i = oldIndex + 1; i <= newIndex; i++) {
            category.products[i].sort = i
          }
        } else {
          // 向前移动：中间项的排序号加1
          for (let i = newIndex; i < oldIndex; i++) {
            category.products[i].sort = i + 2
          }
        }

        // 移动当前商品到新位置
        category.products.splice(oldIndex, 1)
        category.products.splice(newIndex, 0, movingProduct)
        // 设置当前商品的新排序号
        movingProduct.sort = newIndex + 1
      }

      resolve({
        code: 0,
        data: null,
        message: 'success'
      })
    }, 300)
  })
}

// 添加商品时自动设置排序号
export function addProduct(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const category = categoryData.find(item => item.id === data.categoryId)
      if (category) {
        // 新商品的排序号为当前最大排序号加1
        const maxSort = Math.max(...category.products.map(p => p.sort || 0), 0)
        const newProduct = {
          id: Date.now(),
          sort: maxSort + 1,
          ...data
        }
        category.products.push(newProduct)
      }
      resolve({
        code: 0,
        data: null,
        message: 'success'
      })
    }, 300)
  })
}

// 更新商品信息时保持排序号不变
export function updateProduct(data) {
  return new Promise((resolve) => {
    setTimeout(() => {
      categoryData.forEach(category => {
        const index = category.products.findIndex(item => item.id === data.id)
        if (index > -1) {
          // 保持原有排序号
          const oldSort = category.products[index].sort
          category.products[index] = { ...data, sort: oldSort }
        }
      })
      resolve({
        code: 0,
        data: null,
        message: 'success'
      })
    }, 300)
  })
}

// 删除商品时更新其他商品的排序号
export function deleteProduct(id) {
  return new Promise((resolve) => {
    setTimeout(() => {
      categoryData.forEach(category => {
        const index = category.products.findIndex(item => item.id === id)
        if (index > -1) {
          // 删除当前商品
          category.products.splice(index, 1)
          // 更新后续商品的排序号
          for (let i = index; i < category.products.length; i++) {
            category.products[i].sort = i + 1
          }
        }
      })
      resolve({
        code: 0,
        data: null,
        message: 'success'
      })
    }, 300)
  })
}

// 根据类别ID获取商品列表
export function getProductsByCategory(categoryId) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const category = categoryData.find(item => item.id === categoryId)
      if (category) {
        // 确保每个商品都有排序号
        category.products.forEach((product, index) => {
          if (!product.sort) {
            product.sort = index + 1
          }
        })
      }
      resolve({
        code: 0,
        data: category ? category.products : [],
        message: 'success'
      })
    }, 300)
  })
}